//
//  ModifyNikNameVC.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/9/9.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit
import SVProgressHUD

class ModifyNikNameVC: BaseViewController {
    
    var userName : String?
    
    
    lazy var bottomView : UIView = {
        
        let temp = UIView(frame: CGRect(x: 0, y: 0, width: KScreenH, height: 32))
        
        let label = UILabel(frame: temp.bounds)
        
        label.font = UIFont.systemFont(ofSize: 14)
        
        label.textColor = UIColor.colorWith(r: 170, g: 170, b: 170)
        
        
        label.text = "4-20个字符,可由中英文,数字组成"
        
        label.frame.origin.x = 8
        
        
        temp.addSubview(label)
        
        return temp
        
    }()
    

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "修改昵称"
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "保存", style: .plain, target: self, action: #selector(saveNikNameInfo))
        
        plainTableView.backgroundColor = UIColor.viewBgGrey()
        
        plainTableView.tableFooterView = bottomView

        
        plainTableView.register(UINib(nibName: StringFromClass(classType: ModifNikNameCell.self), bundle: nil), forCellReuseIdentifier: ModifNikNameCell.identifer())

        // Do any additional setup after loading the view.
    }
    
    
    func saveNikNameInfo() {
        
        view.endEditing(true)
        
        var parmeter  = [String : AnyObject]()
        
        let  temp : ModifNikNameCell = plainTableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! ModifNikNameCell
        
        QL1(temp.nikNameStr)
        
        parmeter["nickname"] = "\(temp.nikNameStr!)" as AnyObject
        
        NetworkTools.shared.sendRequest(method: .POST_METHOD, pathStr: "mobile/index.php?c=member_account&a=save_nickname", parameters: parmeter) { [weak self] (responseObject, error) in
            
            
            if error  == nil{
                
                SVProgressHUD.showSuccess(withStatus: "修改成功")
  
                
                
                self?.perform(#selector(self?.sussesPop), with: nil, afterDelay: 0.5)
                
                
                
            }else{
                
                SVProgressHUD.showError(withStatus: error?.domain)
                
                
            }
            
        }


        
        
    }
    
    
    
    func sussesPop() {
        
        SVProgressHUD.dismiss()
        
        navigationController?.popViewController(animated: true)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

  
}





// MARK:- tableview代理方法
extension ModifyNikNameVC{
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        
        return 1
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return 1
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let tableCell : ModifNikNameCell  = tableView.dequeueReusableCell(withIdentifier: ModifNikNameCell.identifer()) as! ModifNikNameCell
        
        tableCell.nikNameStr = userName
        
        
        return tableCell
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

            return 46
            
       
        
    }
    
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        view.endEditing(true)
    }
    
    
    
    
    
    
}


